﻿/****************
 * Copyright 2016 Brian Tabone
 * Licensed under the Apache 2.0 License
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * World3DDal.cs
 * Data Access Layer for the World
 * 9/11/2016
 */

using System.Data;
using IAO.Common.ServiceFramework.DataAccessLayer;
using log4net;

namespace World3D.DataAccessLayer
{
    public class World3DDal : IAODal
    {
        public static readonly int SchemaVersion = 1;

        public World3DDal(ILog logger) :
            base(logger, "World3D.db3", SchemaVersion)
        {
        }

        /// <summary>
        ///     Create the schema for World3D
        /// </summary>
        protected override void CreateSchema()
        {
            base.CreateSchema();

            // Create the TrackedEntities table, each entity will have its own properties, which will parent to this entity. 
            using (IDbCommand createSchemaCommand = SqlLiteConnection.CreateCommand())
            {
                createSchemaCommand.CommandType = CommandType.Text;
                createSchemaCommand.CommandText =
                    $@"CREATE TABLE TrackedEntities
                    (
                        Id INT PRIMARY KEY,
                        GUID TEXT NOT NULL,
                        EntityClass TEXT NOT NULL,
                        CreateDT DATETIME NOT NULL,
                        EndDT DATETIME NULL                                      
                    )
                    ";
                createSchemaCommand.ExecuteNonQuery();
            }

            Logger.Info($"World3D Schema Created"); 
        }
    }
}